/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.je.justforme;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import org.je.justforme.http.DefaultProperties;
import org.je.justforme.http.HttpHelper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author jeddy
 */
public class OfferManager {
    private Logger log = Logger.getLogger(OfferManager.class.getSimpleName());
    private HttpHelper helper = new HttpHelper();
    private Properties props = DefaultProperties.getDefaultProps();
    private Map<String, JSONObject> allOffers = null;

    public void login(String userName, String password) throws Exception{
        props.put(DefaultProperties.PROP_UNAME, userName);
        props.put(DefaultProperties.PROP_PASSWORD, password);
        helper.login();
    }

    public Map<String, JSONObject> getAllOffers(){
        if (allOffers == null){
            allOffers = new HashMap<String, JSONObject>();
            allOffers.put(DefaultProperties.PROP_CLUB_SPECIALS_URL, getOffers(props.getProperty(DefaultProperties.PROP_CLUB_SPECIALS_URL)));
            allOffers.put(DefaultProperties.PROP_COUPON_CENTER_URL, getOffers(props.getProperty(DefaultProperties.PROP_COUPON_CENTER_URL)));
            allOffers.put(DefaultProperties.PROP_PERSONALIZED_DEALS_URL, getOffers(props.getProperty(DefaultProperties.PROP_PERSONALIZED_DEALS_URL)));
        }
        return allOffers;
    }
    
    public int addAllOffers() throws Exception{
        int addCount = 0;
        if (allOffers == null){
            getAllOffers();
        }
        for (String key:allOffers.keySet()){
            JSONObject adds = createAddItems(allOffers.get(key));
            addCount += adds.getJSONArray("clips").length();
            addOffers(adds);
        }
        return addCount;
    }

    public JSONObject getOffers(String url) {
        url = url.replace("$time", System.currentTimeMillis() + "");
        return helper.getJson(url);
    }
    
    public int getNumberOfOffersNotSelected()throws JSONException{
        int cnt = 0;
        if (allOffers == null){
            getAllOffers();
        }
        for (String key:allOffers.keySet()){
            JSONObject json = allOffers.get(key);
            JSONArray offers = json.getJSONArray("offers");
            for (int i = 0; i < offers.length(); i++) {
                JSONObject offer = offers.getJSONObject(i);
                String clipStatus = offer.getString("clipStatus");
                if (!"C".equals(clipStatus)) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
    
    public JSONObject createAddItems(JSONObject offerArray) throws JSONException {
        JSONArray offers = offerArray.getJSONArray("offers");
        JSONObject addObjects = new JSONObject();
        JSONArray addList = new JSONArray();
        addObjects.put("clips", addList);
        for (int i = 0; i < offers.length(); i++) {
            JSONObject offer = offers.getJSONObject(i);
            long offerId = offer.getLong("offerId");
            String clipStatus = offer.getString("clipStatus");
            String offerPgm = offer.getString("offerPgm");
            if ("C".equals(clipStatus)) {
                //log.info("offerid: " + offerId + " has already been added");
            } else {
                //log.info("prepare to add offerid: " + offerId);
                //System.out.println(offer.toString(2));
                JSONObject offerAdd = new JSONObject();
                offerAdd.put("offerId", offerId);
                offerAdd.put("offerPgm", offerPgm);
                addList.put(offerAdd);
            }


        }
        return addObjects;
    }

    public void addOffers(JSONObject addObjects) {
        try {
            String postUrl = "http://www.vons.com/Clipping1/services/clip/offers";
            helper.postJson(postUrl, addObjects);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        OfferManager o = new OfferManager();
    }
}
